package com.rookie.mystock.stock.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "stock")
public class StockController {
    private final static String JUHE_SZ_STOCK_LIST_URL = "http://web.juhe.cn:8080/finance/stock/szall?key={key}&page={pageNo}";
    private final static String JUHE_SH_STOCK_LIST_URL = "http://web.juhe.cn:8080/finance/stock/shall?key={key}&page={pageNo}";

    @Value("${juhe.appkey}")
    private String juheAppKey;

    private final RestTemplate restTemplate;

    public StockController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    // 访问聚合数据获取A股数据
    @ResponseBody
    @RequestMapping(value = "getStockList")
    @Cacheable
    public String getStock(){
        Map<String, Object> attrMap = new HashMap();
        attrMap.put("key", juheAppKey);
        attrMap.put("pageNo",1);
        String forObject = restTemplate.getForObject(JUHE_SH_STOCK_LIST_URL, String.class, attrMap);
        boolean json = JSONUtil.isJson(forObject);

        return forObject;
    }
}
